#include "Camera.h"

using namespace GalliumEngine;

Camera::Camera(void)// : ComponentOld("MISSING", RENDER_TYPE::IMMEDIATE)
{
}

Camera::Camera(std::string _id)// : ComponentOld(_id, RENDER_TYPE::IMMEDIATE)
{
}

Camera::~Camera(void)
{
}

std::string Camera::getId()
{
	return id;
}

void Camera::initialize(float _left, float _right, float _bottom, float _top, float _nearPlane, float _farPlane)
{
	//ComponentOld::initialize();

	this->left = _left;
	this->right = _right;
	this->top = _bottom;
	this->bottom = _top;
	this->nearPlane = _nearPlane;
	this->farPlane = _farPlane;
	cameraType = CAMERATYPE_ORTHO;

	rotationSpeed = Vector3(0.00001f,0.00001f,0.00001f);
	velocity = Vector3(0.00001f,0.00001f,0.00001f);

	projection = Matrix4(false);

	rotateMinusX = false;
	rotateX = false;
	rotateMinusY = false;
	rotateY = false;
	rotateMinusZ = false;
	rotateZ = false;
	translateMinusX = false;
	translateX = false;
	translateMinusY = false;
	translateY = false;
	translateMinusZ = false;
	translateZ = false;

	projection.makeOrthoProjection(_left,_right,_bottom,_top,_nearPlane,_farPlane);
}

void Camera::initialize(Vector3 _position, float _fov, float _aspect, float _nearPlane, float _farPlane)
{
	//ComponentOld::initialize();

	this->fov = _fov;
	this->aspect = _aspect;
	this->nearPlane = _nearPlane;
	this->farPlane = _farPlane;
	cameraType = CAMERATYPE_PERSPECTIVE;

	rotationSpeed = Vector3(1,1,1);
	velocity = Vector3(1,1,1);

	projection = Matrix4(true);

	rotateMinusX = false;
	rotateX = false;
	rotateMinusY = false;
	rotateY = false;
	rotateMinusZ = false;
	rotateZ = false;
	translateMinusX = false;
	translateX = false;
	translateMinusY = false;
	translateY = false;
	translateMinusZ = false;
	translateZ = false;
	
	//this->setTranslation(_position);

	projection.makePerspectiveProjection(_fov,_aspect,_nearPlane,_farPlane);
}

void Camera::deinitialize()
{
	//ComponentOld::deinitialize();
}

Matrix4 Camera::getProjectionMatrix()
{
	return projection;
}

void Camera::lookAt(Vector3 eye, Vector3 center, Vector3 up)
{
	/*Vector3 f = (center - eye).normalize();
	Vector3 u = up.normalize();
	Vector3 s = (f.cross(u)).normalize();
	u = s.cross(f);

	Matrix4 result;
	result = Matrix4(s.getX(),
		u.getX(),
		-f.getX(),
		-s.dot(eye),
		s.getY(),
		u.getY(),
		-f.getY(),
		-u.dot(eye),
		s.getZ(),
		u.getZ(),
		-f.getZ(),
		f.dot(eye),
		0, 0, 0, 1);

	Matrix3 rs2 = result.toMatrix3();

	this->setRotation(Quaternion::fromRotationMatrix(rs2));
	this->setRight(s);
	this->setUp(u);
	this->setDirection(-f);
	this->setTranslation(eye);
	this->setScale(1.0f);*/
}

void Camera::update(float delta)
{
	if(cameraType == CAMERATYPE_PERSPECTIVE)
	{
		float rotX = 0.0f;
		float rotY = 0.0f;
		float rotZ = 0.0f;

		/*if(Keyboard::isKeyDown(Keys::Q) || Keyboard::isKeyHeld(Keys::Q))
		{ rotateZ = true; }
		else { rotateZ = false; }

		if(Keyboard::isKeyDown(Keys::E) || Keyboard::isKeyHeld(Keys::E))
		{ rotateMinusZ = true; }
		else { rotateMinusZ = false; }

		if(Keyboard::isKeyDown(Keys::W) || Keyboard::isKeyHeld(Keys::W))
		{ rotateY = true; }
		else { rotateY = false; }

		if(Keyboard::isKeyDown(Keys::S) || Keyboard::isKeyHeld(Keys::S))
		{ rotateMinusY = true; }
		else { rotateMinusY = false; }

		if(Keyboard::isKeyDown(Keys::A) || Keyboard::isKeyHeld(Keys::A))
		{ rotateMinusX = true; }
		else { rotateMinusX = false; }

		if(Keyboard::isKeyDown(Keys::D) || Keyboard::isKeyHeld(Keys::D))
		{ rotateX = true; }
		else { rotateX = false; }*/

		/*if(Keyboard::isKeyDown(Keys::Left) || Keyboard::isKeyHeld(Keys::Left))
		{ rotateMinusY = true; }
		else { rotateMinusY = false; }

		if(Keyboard::isKeyDown(Keys::Right) || Keyboard::isKeyHeld(Keys::Right))
		{ rotateY = true; }
		else { rotateY = false; }

		if(Keyboard::isKeyDown(Keys::Up) || Keyboard::isKeyHeld(Keys::Up))
		{ translateMinusZ = true; }
		else { translateMinusZ = false; }

		if(Keyboard::isKeyDown(Keys::Down) || Keyboard::isKeyHeld(Keys::Down))
		{ translateZ = true; }
		else { translateZ = false; }*/

		/*if(Keyboard::isKeyDown(Keys::RBracket) || Keyboard::isKeyHeld(Keys::RBracket))
		{ translateMinusZ = true; }
		else { translateMinusZ = false; }

		if(Keyboard::isKeyDown(Keys::LBracket) || Keyboard::isKeyHeld(Keys::LBracket))
		{ translateZ = true; }	
		else { translateZ = false; }*/
	}

	//if(rotateMinusX) { this->rotateObjectAroundX(MathUtils::toRadians(-rotationSpeed[0])); }

	//if(rotateX) { this->rotateObjectAroundX(MathUtils::toRadians(rotationSpeed[0])); }

	//if(rotateMinusY) { this->rotateObjectAroundY(MathUtils::toRadians(-rotationSpeed[1])); }

	//if(rotateY) { this->rotateObjectAroundY(MathUtils::toRadians(rotationSpeed[1])); }

	//if(rotateMinusZ) { this->rotateObjectAroundZ(MathUtils::toRadians(-rotationSpeed[2])); }

	//if(rotateZ) { this->rotateObjectAroundZ(MathUtils::toRadians(rotationSpeed[2])); }

	//if(translateMinusX) { this->translateObject(-velocity[0],0,0); }

	//if(translateX) { this->translateObject(velocity[0],0,0); }

	//if(translateMinusY) { this->translateObject(0,-velocity[1],0); }

	//if(translateY) { this->translateObject(0,velocity[1],0); }

	//if(translateMinusZ) { this->translateObject(0,0,-velocity[2]); }

	//if(translateZ) { this->translateObject(0,0,velocity[2]); }

	//ComponentOld::update(delta);
}

void Camera::draw()
{
	//ComponentOld::draw();
}
